package com.minivision.fdiot.util;

import org.springframework.cloud.client.ServiceInstance;

public final class ServiceUtils {
  
  /**
   * 获取服务地址
   * @param instance
   * @return
   */
  public static String getServiceUrl(ServiceInstance instance) {
    String protocol = instance.isSecure() ? "https" : "http";
    return protocol + "://" + getServiceNode(instance);
  }
  
  /**
   * 获取服务节点地址
   * @param instance
   * @return
   */
  public static String getServiceNode(ServiceInstance instance) {
    String ip = IPUtils.getServerIp(instance.getHost());
    ip = ip == null ? instance.getHost() : ip;
    int port = instance.getPort();
    return ip + ":" + port;
  }

}
